﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.Linq;
using WX.WMS.DataModel;
using WX.WMS.Common;
using WX.WMS.BLL;
using System.Drawing;

public partial class StockTake_StockTakeVerify : PageBase
{
    private string _StockTakeID
    {
        get
        {
            if (ViewState["StockTakeID"] == null) return "";
            else return ViewState["StockTakeID"].ToString();
        }
        set { ViewState["StockTakeID"] = value; }
    }

    private string _StockTakeDetailID
    {
        get
        {
            if (ViewState["StockTakeDetailID"] == null) return "";
            else return ViewState["StockTakeDetailID"].ToString();
        }
        set { ViewState["StockTakeDetailID"] = value; }
    }

    [Serializable]
    public class OverRageDetail
    {
        public System.Guid ID { get; set; }

        public System.Nullable<System.Guid> STOCKTAKE_ID { get; set; }

        public bool OVERAGE { get; set; }

        public double PACKET_QTY { get; set; }

        public double PACKET_QTY2 { get; set; }

        public double NET_WEIGHT { get; set; }

        public double GROSS_WEIGHT { get; set; }

        public System.Nullable<System.Guid> STOREID { get; set; }

        public double PACKET_QTY_VERIFY { get; set; }

        public double PACKET_QTY2_VERIFY { get; set; }

        public double NET_WEIGHT_VERIFY { get; set; }

        public double GROSS_WEIGHT_VERIFY { get; set; }
    }

    protected List<OverRageDetail> _OverRageList 
    {
        get
        {
            if (ViewState["OverRageList"] == null)
                ViewState["OverRageList"] = new List<OverRageDetail>();
            return (List<OverRageDetail>)ViewState["OverRageList"];
        }
        set 
        {
            ViewState["OverRageList"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            _StockTakeID = Request.QueryString["ID"];

            BoundDetails(this._StockTakeID);
        }
    }
    protected void SearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SearchResult.PageIndex = e.NewPageIndex;
        BoundDetails(this._StockTakeID);
    }

    protected void BoundDetails(string ID)
    {
        StockTakeBLL stockTakeBLL = new StockTakeBLL();
        SearchResult.DataSource = stockTakeBLL.GetStockTakeByStockTakeID(ID).T_STOCKTAKE_DETAILs;
        SearchResult.DataBind();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string[] results = this.Result.Text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        double packetQty = Convert.ToDouble(results[0]);
        double packetQty2 = Convert.ToDouble(results[1]);
        double netWeight = Convert.ToDouble(results[2]);
        double grossWeight = Convert.ToDouble(results[3]);

        StockTakeDetailBLL stockTakeDetailBLL = new StockTakeDetailBLL();

        T_STOCKTAKE_DETAIL stoclTakeDetail = stockTakeDetailBLL.GetStockTakeDetailByID(this._StockTakeDetailID);

        T_STOCKTAKE_DETAIL updateDetail = new T_STOCKTAKE_DETAIL();

        updateDetail.PACKET_QTY = packetQty;
        updateDetail.PACKET_QTY2 = packetQty2;
        updateDetail.NET_WEIGHT = netWeight;
        updateDetail.GROSS_WEIGHT = grossWeight;

        if (stoclTakeDetail.T_STORE.PACKET_QTY == packetQty && stoclTakeDetail.T_STORE.PACKET_QTY2 == packetQty2
            && stoclTakeDetail.T_STORE.NET_WEIGHT == netWeight && stoclTakeDetail.T_STORE.GROSS_WEIGHT == grossWeight)
        {
            updateDetail.OVERAGE = false;
        }
        else
        {
            updateDetail.OVERAGE = true;

            //OverRageDetail overRageDetail = new OverRageDetail();
            //overRageDetail.GROSS_WEIGHT = stoclTakeDetail.T_STORE.GROSS_WEIGHT;
            //overRageDetail.ID = stoclTakeDetail.ID;
            //overRageDetail.NET_WEIGHT = stoclTakeDetail.T_STORE.NET_WEIGHT;
            //overRageDetail.OVERAGE = true;
            //overRageDetail.PACKET_QTY = stoclTakeDetail.T_STORE.PACKET_QTY;
            //overRageDetail.PACKET_QTY2 = stoclTakeDetail.T_STORE.PACKET_QTY2;
            //overRageDetail.STOCKTAKE_ID = stoclTakeDetail.STOCKTAKE_ID;
            //overRageDetail.STOREID = stoclTakeDetail.STOREID;
            //overRageDetail.GROSS_WEIGHT_VERIFY = grossWeight;
            //overRageDetail.NET_WEIGHT_VERIFY = netWeight;
            //overRageDetail.PACKET_QTY_VERIFY = packetQty;
            //overRageDetail.PACKET_QTY2_VERIFY = packetQty2;

            //this._OverRageList.Add(overRageDetail);
        }

        stockTakeDetailBLL.UpdateStockTakeDetail(updateDetail);

        BoundDetails(this._StockTakeID);
    }

    protected void BoundOverRageGridView()
    {

    }
    protected void SearchResult_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Visible = false;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[7].Attributes.Add("onclick", " return yinyong_onclick();");
            }
        }
    }
    protected void SearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[0].Text.ToLower() == "true")
            {
                e.Row.BackColor = Color.OrangeRed;
            }
        }
    }
    protected void SearchResult_RowEditing(object sender, GridViewEditEventArgs e)
    {
        _StockTakeDetailID = SearchResult.DataKeys[e.NewEditIndex]["ID"].ToString();
        //Jscript.RunJs("yinyong_onclick()");
        //Jscript.ShowModalDialogWindow("VerifyInput.aspx", "300,300,300,300");
        //ClientScript.RegisterClientScriptBlock(this.GetType(), "", "yinyong_onclick();");

        string[] results = this.Result.Text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        double packetQty = Convert.ToDouble(results[0]);
        double packetQty2 = Convert.ToDouble(results[1]);
        double netWeight = Convert.ToDouble(results[2]);
        double grossWeight = Convert.ToDouble(results[3]);

        StockTakeDetailBLL stockTakeDetailBLL = new StockTakeDetailBLL();

        T_STOCKTAKE_DETAIL stoclTakeDetail = stockTakeDetailBLL.GetStockTakeDetailByID(this._StockTakeDetailID);

        stoclTakeDetail.PACKET_QTY = packetQty;
        stoclTakeDetail.PACKET_QTY2 = packetQty2;
        stoclTakeDetail.NET_WEIGHT = netWeight;
        stoclTakeDetail.GROSS_WEIGHT = grossWeight;

        if (stoclTakeDetail.T_STORE.PACKET_QTY == packetQty && stoclTakeDetail.T_STORE.PACKET_QTY2 == packetQty2
            && stoclTakeDetail.T_STORE.NET_WEIGHT == netWeight && stoclTakeDetail.T_STORE.GROSS_WEIGHT == grossWeight)
        {
            stoclTakeDetail.OVERAGE = false;
        }
        else
        {
            stoclTakeDetail.OVERAGE = true;

            //OverRageDetail overRageDetail = new OverRageDetail();
            //overRageDetail.GROSS_WEIGHT = stoclTakeDetail.T_STORE.GROSS_WEIGHT;
            //overRageDetail.ID = stoclTakeDetail.ID;
            //overRageDetail.NET_WEIGHT = stoclTakeDetail.T_STORE.NET_WEIGHT;
            //overRageDetail.OVERAGE = true;
            //overRageDetail.PACKET_QTY = stoclTakeDetail.T_STORE.PACKET_QTY;
            //overRageDetail.PACKET_QTY2 = stoclTakeDetail.T_STORE.PACKET_QTY2;
            //overRageDetail.STOCKTAKE_ID = stoclTakeDetail.STOCKTAKE_ID;
            //overRageDetail.STOREID = stoclTakeDetail.STOREID;
            //overRageDetail.GROSS_WEIGHT_VERIFY = grossWeight;
            //overRageDetail.NET_WEIGHT_VERIFY = netWeight;
            //overRageDetail.PACKET_QTY_VERIFY = packetQty;
            //overRageDetail.PACKET_QTY2_VERIFY = packetQty2;

            //this._OverRageList.Add(overRageDetail);
        }

        stockTakeDetailBLL.UpdateStockTakeDetail(stoclTakeDetail);

        BoundDetails(this._StockTakeID);
    }
}
